using System.Collections.Generic;
using Framework.Core;
using UnityEngine;
using UnityEngine.Audio;

namespace Framework
{
    public class AudioParams
    {
        public int SoundId { get; private set; }
        public Transform FollowTarget { get; private set; }
        public bool ParamsChanged { get; private set; }
        public bool Loop;
        public float NormalizeTime;
        private object _sign;
        private Dictionary<string, float> _params;
        public AudioParams(object sign)
        {
            _sign = sign;
            Loop = false;
            NormalizeTime = 0;
            _params = new Dictionary<string, float>();
        }

        public void Initialize(int id, object sign)
        {
            if (_sign != sign) return;
            this.SoundId = id;
            NormalizeTime = 0;
            Loop = false;
            FollowTarget = null;
            _params.Clear();
        }
        public void SetFollowTarget(Transform value)
        {
            FollowTarget = value;
        }
        public void SetFloat(string name, float value)
        {
            if (_params.ContainsKey(name))
            {
                _params[name] = value;
            }
            else
            {
                _params.Add(name, value);
            }
            ParamsChanged = true;
        }
        public void ReadParams(IAudioAgent value)
        {
            if (value == null) return;
            if (!ParamsChanged) return;
            ParamsChanged = false;
            foreach (var item in _params)
            {
                value.SetFloat(item.Key, item.Value);
            }
            _params.Clear();
        }
    }
}